﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Filldata(); 
            }
        }


        private void Filldata()
        {
            var sql = "select * from Articles";
            var dt = Dbhelper.table(sql);
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

        private string GetCellValue(int rowIndex, int cellIndex)
        {
            var control = GridView1.Rows[rowIndex].Cells[cellIndex];
            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return res;
        }


        //取消编辑模式
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Filldata();
        }

        //更新模式
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var Id = GetCellValue(e.RowIndex, 0);
            //转换成功为true
            var IsOk = int.TryParse(Id, out int uid);
            var Title = GetCellValue(e.RowIndex, 1);
            var Content = GetCellValue(e.RowIndex, 2);
            var Author = GetCellValue(e.RowIndex, 3);
            var Remarks = GetCellValue(e.RowIndex, 4);
            if (IsOk)
            {
                var sql = string.Format("update Articles set Title ='{0}', Content = '{1}',Author = '{2}',Remarks ='{3}' where Id ={4}", Title , Content, Author, Remarks, Id);
                Dbhelper.AddOrUpdateOrDelete(sql);

            }
            else
            {
                var sql = string.Format("insert into Articles (Title ,Author ,Content,Remarks) values ('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                Dbhelper.AddOrUpdateOrDelete(sql);
            }

            GridView1.EditIndex = -1;
            Filldata();


        }

        //编辑模式
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Filldata();

        }

        //删除模式
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GetCellValue(e.RowIndex, 0);
            var sql = string.Format("delete from Articles where Id ='{0}'", Id);
            Dbhelper.AddOrUpdateOrDelete(sql);
            Filldata();
        }

        //添加按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var temp = Dbhelper.table(sql);

            var dt = temp.Copy();
            dt.Rows.Add(dt.NewRow());

            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = dt;
            GridView1.DataBind();


        }
    }
}